//分类显示
$(".nav-classify").find("li").mouseover(function() {
	if($(this).find(".nav-classify-sub-menu").is(":hidden")) {
		$(this).find(".nav-classify-sub-menu").show();
	}
})	
$(".nav-classify").find("li").mouseout(function() {
	if($(this).find(".nav-classify-sub-menu").is(":visible")) {
		$(this).find(".nav-classify-sub-menu").hide();	
	}
})

//二维码显示
$(".top-phone-icon").mouseover(function() {
	if($(this).find("span").is(":hidden")) {
		$(this).find("span").show();
	}
})	
$(".top-phone-icon").mouseout(function() {
	if($(this).find("span").is(":visible")) {
		$(this).find("span").hide();	
	}
})


if($("#is-index").length == 0){
	//子分类显示
	$(".nav-classify").mouseover(function() {
		if($(this).find("ul").is(":hidden")) {
			$(this).find("ul").show();
			$(this).find(".nav-main-title").find("b").attr("class","on");
		}
	})
	$(".nav-classify").mouseout(function() {
		if($(this).find("ul").is(":visible")) {
			$(this).find("ul").hide();
			$(this).find(".nav-main-title").find("b").attr("class","");
		}
	})
}
//返回顶部
$(document).ready(function() {
	//首先将top隐藏
	$("#top").hide();
	//当滚动条的位置处于距顶部100像素以下时，跳转链接出现，否则消失
	$(function() {
		$(window).scroll(function() {
			if ($(window).scrollTop() > 100) {
				$("#top").fadeIn(250);
			} else {
				$("#top").fadeOut(250);
				
			}
		});
		//当点击跳转链接后，回到页面顶部位置
		$("#top").click(function() {
			$('body,html').animate({
				scrollTop: 0
			},
			300);
			return false;
		});
	});
});

//定位侧边Tools
function scrollx(p) {
 var d = document, dd = d.documentElement, db = d.body, w = window, o = d.getElementById(p.id), ie6 = /msie 6/i.test(navigator.userAgent), style, timer;
 if (o) {
  cssPub = ";position:"+(p.f&&!ie6?'fixed':'absolute')+";"+(p.t!=undefined?'top:'+p.t+'px;':'bottom:10px;');
  if (p.r != undefined && p.l == undefined) {
   o.style.cssText += cssPub + ('right:'+p.r+'px;');
  } else {
   o.style.cssText += cssPub + ('margin-left:'+p.l+'px;');
  }
  if(p.f&&ie6){
   cssTop = ';top:expression(documentElement.scrollTop +'+(p.t==undefined?dd.clientHeight-o.offsetHeight:p.t)+'+ "px" );';
   cssRight = ';right:expression(documentElement.scrollright + '+(p.r==undefined?dd.clientWidth-o.offsetWidth:p.r)+' + "px")';
   if (p.r != undefined && p.l == undefined) {
	o.style.cssText += cssRight + cssTop;
   } else {
	o.style.cssText += cssTop;
   }
   dd.style.cssText +=';background-image: url(about:blank);background-attachment:fixed;';
  }else{
   if(!p.f){
	w.onresize = w.onscroll = function(){
	 clearInterval(timer);
	 timer = setInterval(function(){
	  //双选择为了修复chrome 下xhtml解析时dd.scrollTop为 0
	  var st = (dd.scrollTop||db.scrollTop),c;
	  c = st - o.offsetTop + (p.t!=undefined?p.t:(w.innerHeight||dd.clientHeight)-o.offsetHeight);
	  if(c!=0){
	   o.style.top = o.offsetTop + Math.ceil(Math.abs(c)/10)*(c<0?-1:1) + 'px';
	  }else{
	   clearInterval(timer);
	  }
	 },10)
	}
   }
  }
 }
}
scrollx({id:'tools', l:1000, f:1});

//侧边栏顶部广告跟随滚动条固定在顶部效果
$(function(){$(function() {
    //检查对象，#boxs是要随滚动条固定的ID
    var offset = 200;
    $(window).scroll(function () {
    //检查对象的顶部是否在游览器可见的范围内
        var scrollTop = $(window).scrollTop();
            if (offset < scrollTop){
                $('#nav-wrap').addClass('fixed');
            }
            else{
                $('#nav-wrap').removeClass('fixed');
            } 
        });
    });
}); 

//菜篮子
var timer;
//clearTimeout(timer);
$(".nav-shopping-cart").mouseenter(function() {
	clearTimeout(timer);
	if($(this).find(".nav-shopping-cart-content").is(":hidden")) {
		$(this).find(".nav-shopping-cart-content").show();
		$(this).find(".nav-shopping-cart-title").find("b").attr("class","on");
	}	
})	
$(".nav-shopping-cart").mouseleave(function() {
	var t = $(this);
	timer = setTimeout(function(){
		if($(t).find(".nav-shopping-cart-content").is(":visible")) {
			$(t).find(".nav-shopping-cart-content").hide();
			$(t).find(".nav-shopping-cart-title").find("b").attr("class","");
		}
	},200);
})

//新建cookie
var COOKIE_NAME = 'cart';
$(document).ready(function(){
	//如果有记录进行回显
	if( $.cookie(COOKIE_NAME) ){
		var $param = eval($.cookie(COOKIE_NAME));
		$($param).each(function() {
			$countPrice = parseFloat(this.count) * parseFloat(this.price);
			$countPrice = $countPrice.toFixed(2);
			var html = "<div class='lunch-list'>";
			html += "<span class='lunch-name'><img src='"+this.img+"'/></span><span class='lunch-count'><a href='javascript:void(0)' class='count-btn count-minus' onclick='cartMinus(this)'>-</a><input class='count-text goTo' type='text' name='nums[]' value='"+this.count+"' readonly maxlength='5'/><a href='javascript:void(0)' class='count-btn count-plus' onclick='cartPlus(this)'>+</a>";
			html += "</span><span class='lunch-price'>￥<font class='price'>"+$countPrice+"</font><i style='display:none;' class='temp-price'>"+this.price+"</i></span><span class='lunch-handle'>";
			html += "<input type='hidden' name='ids[]' class='cart-id' value='"+this.id+"'/>";
			html += "<a href='javascript:void(0)' title='删除' class='lunch-del' onclick='cartDel(this)'>×</a></span></div>";
			$("#lunch-list-box").append(html);
		})
	}
	countPrices();
	var $plus = $(".count-plus");
	var $minus = $(".count-minus");
	var $count = $(".count-text");
	var $del = $(".lunch-del");
	var $addToCarts = $(".addToCart");
	var $lunchList = $("#lunch-list-box");
	//加入购物车效果
	$(".addToCart").click(function(event) {
		var t = $(this);
    	var offset = $(".shop-icon-car").offset();
        var flyer = $('<i class="shop-flyer-icon"></i>');//抛物体对象
        var iTop = ($(this).offset().top - $(window).scrollTop()) - 20;
		var iLeft = $(this).offset().left + 10;
		var offsetTop = 20;
		if($("#nav-wrap").attr("class") == "nav-wrap"){
			offsetTop = $(".shop-icon-car").offset().top;
		}
        flyer.fly({
            start: {
                left: iLeft, //抛物体起点横坐标
                top: iTop////抛物体起点纵坐标
            },
            end: {
                left: offset.left, //抛物体终点横坐标
                top: offsetTop, //抛物体终点纵坐标
            },
            onEnd: function() {
                this.destory();//销毁抛物体
                addToCarts(t);
            },
            speed: 2.5
        });
    });
	
	
	//把列表中的商品添加到购物车内
	function addToCarts(t) {
		$img = $(t).parents(".m-floor-content-item").find(".m-floor-content-photo a img").attr("src");//图片
		$num = 1;//数量
		$tempPrice = $(t).parents(".m-floor-content-item").find(".m-floor-content-price font").html();//价格
		$price = parseFloat($tempPrice) * parseFloat($num);//商品乘以数量的价格
		$id = $(t).parents(".m-floor-content-item").find(".product-id").val();
		$tempPrice = parseFloat($tempPrice).toFixed(2);
		$price = $price.toFixed(2);
		addCart($num,$tempPrice,$price,$id,$img);
	}
	
	//加入购物车函数
	function addCart($num,$tempPrice,$price,$id,$img){
		$temp = true;
		//判断如果有该套餐则只进行增加数量和价格操作
		if($(".cart-id").val() != null){
			$(".cart-id").each(
				function() {
					if($id == $(this).val()){
						$temp = false;
						$cartNum = $(this).parents(".lunch-list").find(".count-text");
						$cartPrice = $(this).parents(".lunch-list").find(".price");
						$finalNum = parseFloat($cartNum.val()) + 1;
						$cartNum.val($finalNum);
						var $fPrice = $finalNum * parseFloat($tempPrice);
						$cartPrice.html($fPrice.toFixed(2));
					}
				}
			)
		}
		if($temp == true){
			var html = "<div class='lunch-list'>";
			html += "<span class='lunch-name'><img src='"+$img+"'/></span><span class='lunch-count'><a href='javascript:void(0)' class='count-btn count-minus' onclick='cartMinus(this)'>-</a><input class='count-text goTo' type='text' name='nums[]' value='"+$num+"' readonly maxlength='5'/><a href='javascript:void(0)' class='count-btn count-plus' onclick='cartPlus(this)'>+</a>";
			html += "</span><span class='lunch-price'>￥<font class='price'>"+$price+"</font><i style='display:none;' class='temp-price'>"+$tempPrice+"</i></span><span class='lunch-handle'>";
			html += "<input type='hidden' name='ids[]' class='cart-id' value='"+$id+"'/>";
			html += "<a href='javascript:void(0)' title='删除' class='lunch-del' onclick='cartDel(this)'>×</a></span></div>";
			$lunchList.append(html);
		}
		countPrices();
	}
});
function doout(){
	window.location.href= "<{:U('Index/Login/logout')}>";
}
//增加订单数量
function cartPlus(t){
	var $priceNum = $(t).prev("input");
	var $toTalNum = parseFloat($priceNum.val()) + 1;
	$priceNum.val($toTalNum);
	var $thisPrice = $(t).parent().next().children("font");
	var $thisCountNum =  $(t).parent().next().children("i");
	var $cPrice = parseFloat($priceNum.val()) * parseFloat($thisCountNum.html());
	$thisPrice.html($cPrice.toFixed(2));
	countPrices();
}

//减少订单数量
function cartMinus(t){
	var $priceNum = $(t).next("input");
	if(parseFloat($priceNum.val()) > 1){
		var $toTalNum = parseFloat($priceNum.val()) - 1;
		$priceNum.val($toTalNum);
		var $thisPrice = $(t).parent().next().children("font");
		var $thisCountNum =  $(t).parent().next().children("i");
		var $cPrice = parseFloat($priceNum.val()) * parseFloat($thisCountNum.html())
		$thisPrice.html($cPrice.toFixed(2));
	}else{
		$(t).parent().parent().remove();
	}
	countPrices();
}

//删除商品
function cartDel(t){
	$(t).parent().parent().remove();
	countPrices();
}

//清空所有商品
function clearCar() {
	$("#lunch-list-box").html("");
	countPrices();
}

//计算最终的数量和价格和cookie操作
function countPrices(){
	var $toTalPrice = 0;
	var $toTalNum = 0;
	$(".count-text").each(function() {
		if(!isNaN($(this).val())){
			$toTalNum = parseFloat($(this).val()) + $toTalNum;
		}
	});
	$(".price").each(function() {
		if(!isNaN($(this).html())){
			$toTalPrice = parseFloat($(this).html()) + $toTalPrice;
		}
	});
	$toTalPrice = $toTalPrice.toFixed(2);
	$(".total-price").html($toTalPrice);
	$(".total-num").html($toTalNum);
	if(parseFloat($toTalPrice) > 0) {
		$(".settlement-box").show();
		$(".no-param-box").hide();
	}else{
		$(".settlement-box").hide();
		$(".no-param-box").show();
	}
	if(parseFloat($toTalPrice) <= 0) {
		$("#settlement").attr("disabled", true);
		$("#settlement").attr("class","settlement-off");
		$("#settlement").val("立即结算");
	}else{
		$("#settlement").attr("disabled", false);
		$("#settlement").attr("class","settlement");
		$("#settlement").val("立即结算");
	}
	
	$countToTalPrice = parseFloat($toTalPrice);
	//$countToTalPrice = parseFloat($toTalPrice) + parseFloat($(".fastmail-price").html());
	$countToTalPrice = $countToTalPrice.toFixed(2);
	$(".count-total-price").html($countToTalPrice);
	
	//将购物车数据转换为json格式
	var cartTemp = new Object();
	var cartItem = "[";
	$(".lunch-list").each(
		function() {
			$cookieImg = $(this).find(".lunch-name img").attr("src");
			$cookieNum = $(this).find(".count-text").val();
			$cookiePrice = $(this).find(".temp-price").html();
			$cookieId = $(this).find(".cart-id").val();
			cartTemp.img = $cookieImg;
			cartTemp.count = $cookieNum;
			cartTemp.price = $cookiePrice;
			cartTemp.id = $cookieId;
			if(cartItem == "["){
				cartItem = cartItem + JSON.stringify(cartTemp);
			}else{
				cartItem = cartItem +","+JSON.stringify(cartTemp);
			}
		}
	)
	cartItem = cartItem+"]";
	//cookie操作
	$.cookie(COOKIE_NAME, null, { path: '/' });
	if(cartItem != "[]"){
		$.cookie(COOKIE_NAME, cartItem , { path: '/', expires: 30 });
	}
}

//封装后的弹窗控件方法
function layerDialog(title,url,width,height,shadeClose) {/*title:dialog的标题,url:dialog的路径,width:dialog的宽度,height:dialog的高度,shadeClose:布尔值，点击遮罩层是否关闭当前窗口*/
	layer.open({
		type: 2,
	    shade: [0.4, '#000'],
	    fix: true,
	    move: false,
	    moveType: 1,
	    title:[title],
	    scrollbar: true,
	    area: [width , height],
	    content: url
	});
}

